/**
 * HopeCMS 提示框JS
 * @author x.mo (root@imoi.cn)
 */
var dpAlert = (function(){

	var interval = null; var timer = 0;

	lock = function () {
		lock_html = '<div id="layout_lock"></div>';
		$('body').append(lock_html);
		$('#layout_lock').show();
	}

	buttons = function() {
		b = '<input type="button" id="button_close" value="关闭" />';
		return b;
	}

	ok = function() {
		o = '<input type="button" id="ok_button" value="确定" />';
		return o;
	}

	alerts = function (title, content, h, w) {
		alert_html = '<div id="alert">' + contents(title, content, w) + '</div>';
		$('body').append(alert_html);
		close_html = '<div id="close"></div>';
		$('#alert').append(close_html);
		$('#dpa_content_box').css('height', h);
		$('#button_close').focus();
		$('#alert').css({top:'-200px'}).animate({top:'200px', height: h}, 200);
	}

	notices = function (content, time) {
		var intertime = null;
		var i = time;
		$('body').append('<div id="notice"></div>');
		$('body').append('<div id="notice_content">'+content+'</div>');
		$('#notice').css({top:'-30px'}).animate({top:'0'}, 30);
		$('#notice_content').css({top:'-30px'}).animate({top:'0'}, 30);
		t = function(){
			if (i % 1000 == 0) {
				clearInterval(intertime);
				intertime = null;
				$('#notice').remove();
				$('#notice_content').remove();
			}
		}
		intertime = setInterval(t, time);
	}

	aop = function(a,b,c,d,e,f) {
		alert_html = '<div id="alert">' + op_contents(a,b,c,d,e,f) + '</div>';
		$('body').append(alert_html);
		close_html = '<div id="close"></div>';
		$('#alert').append(close_html);
		$('#dpa_content_box').css('height', 230);
		$('#button_close').focus();
		$('#alert').css({top:'-200px'}).animate({top:'200px', height: '230px'}, 200);
	}

	op_contents = function(a,b,c,d,e,f) {
		cc =  '<div id="dpa_content_box">';
		cc += '<div id="dpa_title">' + a + '</div>';
		cc += '<div id="dpa_content">' + b + '</div>';
		cc += '<input type="button" id="jump1" class="jbt" value="'+d+'" url="'+c+'" />';
		cc += '<input type="button" id="jump2" class="jbt" value="'+f+'" url="'+e+'" />';
		cc += '</div>';
		return cc;
	}

	contents = function (title, content, w) {
		c = '<div id="dpa_content_box">';
		c += '<div id="dpa_title">' + title + '</div>';
		c += '<div id="dpa_content">' + content + '</div>';
		if(w == true) {
			c += ok();
		}
		c += buttons();
		c += '</div>';
		return c;
	}

	remove = function(callback) {
		var win = false;
		$(document).keyup(function(event) {
			if (event.which == 27) {
				switch (callback) {
					case 'alerts' :
						$('#alert').remove();
						break;
					case 'aop' :
						$('#alert').remove();
						break;
				}
				$('#layout_lock, #close').remove();
			}
		})

		$('#close').click(function(){
			switch (callback) {
				case 'alerts' :
					$('#alert').remove();
					break;
				case 'aop' :
					$('#alert').remove();
					break;
			}
			$('#layout_lock, #close').remove();
		})

		$('#jump1, #jump2').click(function(){
			$('#alert').remove();
			$('#layout_lock, #close').remove();
		})

		$('#ok_button').click(function(){
			switch (callback) {
				case 'alerts' :
					$('#alert').remove();
					break;
			}
			$('#layout_lock, #close').remove();
		})

		$('#button_close').click(function(){
			switch (callback) {
				case 'alerts' :
					$('#alert').remove();
					break;
			}
			$('#layout_lock, #close').remove();
		})
	}

	return function (o, b) {
		var ob = b, win = false;
		ob.callback != 'notice' && lock.call();
		ob.func = function () {
			switch (ob.callback) {
				case 'alerts' :
					if(b.win == true)
						alerts(ob.title, ob.content, ob.height+'px' || '300px', true);
					else
						alerts(ob.title, ob.content, ob.height+'px' || '300px', false);
					break;
				case 'notice' :
					notices(ob.content, ob.time || 2000);
					break;
				case 'aop' :
					aop(ob.title, ob.content, ob.link_1, ob.text_1, ob.link_2, ob.text_2);
					break;
			}
			remove(ob.callback);
		}
		return ob.func();
	}

})();